//
//  block.cpp
//  Survival
//
//  Created by Maxime BACOUX on 11/08/12.
//
//

#include "block.h"
#include "game.h"

Block* Block::blocklist[256];


Block::Block(int id, int texture)
{
	model = Game::getInstance()->getModelManager()->get("cube");
	blockId = id;
	textureIdInsideFile = texture;
	textFile = Game::getInstance()->getTextureManager()->get("texture.bmp");
	renderCubic = true;
		
	blocklist[id] = this;
}

int Block::getFaceTexture(int f)
{
	return textureIdInsideFile;
}

void Block::draw()
{
	this->draw(0,0,0);
}

void Block::draw(int px, int py, int pz)
{
	for(int i = 0; i < model->faces.size(); i++)
	{
		drawFace(i, px, py, pz);
	}
}

void Block::drawFace(int f, int px, int py, int pz)
{
	//glBegin(GL_QUADS);
	//glColor3ub(255,255,255);
	for(int j = 0; j < model->faces[f].size(); j++)
	{
		double x = 0.00, y = 0.00;
			
		if(j > 1) x=1.00;
		if(j == 1 || j == 2) y = 1.00;
			
		x /= 16.10;
		y /= 16.10;
			
		y += 15.00/16.00;
			
		x += (getFaceTexture(f)%16)/16.00;
		y -= (getFaceTexture(f)/16)/16.00;
			
		glTexCoord2d(x,y);
		glVertex3d(model->vertices[model->faces[f][j]-1].x + px, model->vertices[model->faces[f][j]-1].z + pz, model->vertices[model->faces[f][j]-1].y + py);
	}
	//glEnd();
}


void Block::init()
{
	terre = new Block(1,0);
	herbe = new Block(2,1);
	mur = new Block(20,8);
}

bool Block::doesRenderCubic()
{
	return renderCubic;
}



Block* Block::terre = NULL;
Block* Block::herbe = NULL;
Block* Block::mur = NULL;

